﻿#include "frame.h"
#include "string.h"
#include <stdlib.h>
#include <stdio.h>
#include <api_debug.h>
#include <api_os.h>

uchar frame[FRAMELENGTH];

uint Count = 1;

const uchar IMEI[] = { 0x08,0x67,0x56,0x70,0x21,0x47,0x57,0x48 };//设备IMEI号

uint8_t buffer[RECEIVE_BUFFER_MAX_LENGTH];

void login_frame()
{
	int i;
	uchar crc_msg[12];
	unsigned short crc;
	if (frame[0] != '\0')
	{
		memset(frame, '\0', sizeof(frame));
	}

	frame[0] = 0x78;
	frame[1] = 0x78;
	frame[2] = 0x0D;
	frame[3] = 0x01;
	for (i = 0; i < 8; i++)
	{
		frame[i + 4] = IMEI[i];
	}

	frame[12] = (Count & 0xff00) >> 8;
	frame[13] = Count & 0xff;
	for (i = 0; i < 12; i++)
	{
		crc_msg[i] = frame[i + 2];
	}
	crc = GetCrc16(crc_msg, 12);
	frame[14] = (crc & 0xff00) >> 8;
	frame[15] = crc & 0xff;
	frame[16] = 0x0D;
	frame[17] = 0x0A;

}

void status_frame(uint gps)
{
	uchar msg[9];
	unsigned short crc;
	int i;
	if (frame[0] != '\0')
	{
		memset(frame, '\0', sizeof(frame));
	}
	frame[0] = 0x78;
	frame[1] = 0x78;
	frame[2] = 0x08;
	frame[3] = 0x13;

	if (gps) {
		frame[4] = 0x42;//gpsÒÑ¶¨Î»
	}
	else {
		frame[4] = 0x02;//gpsÎ´¶¨Î»
	}
	frame[5] = 0x04;
	frame[6] = 0x03;
	frame[7] = 0x00;
	frame[8] = 0x01;
	frame[9] = (Count & 0xff00) >> 8;
	frame[10] = Count & 0xff;
	for (i = 0; i < 9; i++)
	{
		msg[i] = frame[2 + i];
	}
	crc = GetCrc16(msg, sizeof(msg));
	frame[11] = (crc & 0xff00) >> 8;
	frame[12] = crc & 0xff;
	frame[13] = 0x0d;
	frame[14] = 0x0a;
}



int char2int(char num)
{
	return (int)(num - '0');
}
double mypow(double sum, int n)
{
	double temp = sum;
	if (n != 0)
	{
		for (int i = 1; i < n; i++)
		{
			temp *= sum;
		}
	}
	else
	{
		temp = 1;
	}
	return temp;
}


double char2double(char* str)
{
	double result = 0;
	int i = 0;
	int pointPos;
	int numInt = 0;
	double numFloat = 0;
	while (*(str + i) != '.'&&*(str+i)!='\0')
	{
		if (*(str + i) == '\0')
		{
			return 0;
		}
		i++;
	}
	pointPos = i;
	for (i = 0; i < pointPos; i++)
	{
		//printf_s("%d\n", (char2int(*(str + i))*pow(10, pointPos - i - 1)));
		numInt += char2int(*(str + i))*mypow(10, pointPos - i - 1);
	}

	for (i = pointPos + 1; *(str + i) != '\0'; i++)
	{
		numFloat += char2int(*(str + i))*mypow(0.1, i - pointPos);
	}
	result = numInt + numFloat;
	return result;
}

int string2int(char* str, int length)
{
	int result = 0;
	for (int i = 0; *(str + i) != '\0'; i++)
	{
		result += char2int(*(str + i))*(int)mypow(10, (length - i - 2));
	}
	return result;
}

int myabs(int Num)
{
	if (Num >= 0)
	{
		return Num;
	}
	else {
		return -Num;
	}
}


void gpsFrame(GPS_Information_t* gpsInfo1)
{
	double latitude;
	uint32_t Latitude;
	double longitude;
	uint32_t Longitude;
	uint16_t statusCourse;
	unsigned char msg[22];
	unsigned short crc;


	memset(frame, 0, sizeof(frame));

	frame[0] = 0x78;
	frame[1] = 0x78;
	frame[2] = 0x17;
	frame[3] = 0x10;
	
	frame[4] = char2int(gpsInfo1->UTC_Date[4]) * 10 + char2int(gpsInfo1->UTC_Date[5]);
	frame[5] = char2int(gpsInfo1->UTC_Date[2]) * 10 + char2int(gpsInfo1->UTC_Date[3]);
	frame[6] = char2int(gpsInfo1->UTC_Date[0]) * 10 + char2int(gpsInfo1->UTC_Date[1]);
	frame[9] = char2int(gpsInfo1->UTC_Time[4]) * 10 + char2int(gpsInfo1->UTC_Time[5]);
	frame[8] = char2int(gpsInfo1->UTC_Time[2]) * 10 + char2int(gpsInfo1->UTC_Time[3]);
	frame[7] = char2int(gpsInfo1->UTC_Time[0]) * 10 + char2int(gpsInfo1->UTC_Time[1]);
	
	//Trace(2, "year:%d month:%d day:%d", frame[4], frame[5], frame[6]);
	//Trace(2, "hour:%d min:%d sec:%d", frame[7], frame[8], frame[9]);

	frame[10] = 0xc0 | gpsInfo1->Use_EPH_Sum;//gps信息长度与定位卫星数(卫星数无法获取设置为4)

	latitude = char2double(gpsInfo1->latitude) / 100.0;
	
	Latitude = (int)latitude * 1800000 + (latitude - (int)latitude) * 100 * 30000;
	

	frame[11] = (Latitude & 0xff000000) >> 24;
	frame[12] = (Latitude & 0xff0000) >> 16;
	frame[13] = (Latitude & 0xff00) >> 8;
	frame[14] = Latitude & 0xff;

	longitude = char2double(gpsInfo1->longitude) / 100.0;
	//Trace(2, "long_int:%d", (int)longitude);
	Longitude = (int)longitude * 1800000 + (longitude - (int)longitude) * 100 * 30000;
	//Longitude = 822684240;
	//Latitude = 2279093292;
	
	
	frame[15] = ((Longitude & 0x0f000000) >> 24) & 0x0fffffff;
	frame[16] = (Longitude & 0xff0000) >> 16;
	frame[17] = (Longitude & 0xff00) >> 8;
	frame[18] = Longitude & 0xff;
	//Trace(2, "Longitude:%d", Longitude);
	//Trace(2, "Latitude:%d", Latitude);
	//Trace(2, "Latitude(frame):%x%x%x%x", frame[11], frame[12], frame[13], frame[14]);
	//Trace(2, "Longitude(frame):%2x%2x%2x%2x", frame[15], frame[16], frame[17], frame[18]);

	frame[19] = FliterSpeed;
	
	statusCourse = 0x05 << 10 | myabs((int)(gpsInfo1->Course));//东经北纬

	frame[20] = (statusCourse & 0xff00) >> 8;
	frame[21] = statusCourse & 0xff;
	
	//frame[20] = 0x14;
	//frame[21] = 0x00;//未知bug,course会干扰此字段!!!!!



	frame[22] = (Count & 0xff00) >> 8;
	frame[23] = Count & 0xff;
	for (int i = 0; i < 22; i++)
	{
		msg[i] = frame[i + 2];
		//printf_s("msg:%x\n", frame[i + 2]);
	}
	crc = GetCrc16(msg, 22);
	frame[24] = (crc & 0xff00) >> 8;
	frame[25] = crc & 0xff;
	frame[26] = 0x0d;
	frame[27] = 0x0a;
	//gpsInfo1 = NULL;
	//printf_s("crc:%x\n", crc);
}



//滤波

unsigned char filter()
{
	unsigned char i, j;
	unsigned char temp, sum = 0, value;
	unsigned char value_buf[FliterN];
	Trace(2, "fliter!!!");
	for (i = 0; i<FliterN; i++)
	{
		value_buf[i] = SpeedValue[i];


	}
	//采样值从小到大排列（冒泡法）
	for (j = 0; j<FliterN - 1; j++)
	{
		for (i = 0; i<FliterN - j; i++)
		{
			if (value_buf[i]>value_buf[i + 1])
			{
				temp = value_buf[i];
				value_buf[i] = value_buf[i + 1];
				value_buf[i + 1] = temp;
			}
		}
	}
	for (i = 1; i<FliterN - 1; i++)
		sum += value_buf[i];
	value = sum / (FliterN - 2);
	return(value);
}

